package com.naiterui.ehp.ps.push.utils;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.support.utils.JsonMapper;
import com.naiterui.ehp.bp.utils.lang.ConfigUtil;
import com.naiterui.ehp.bp.utils.net.RestHttpUtil;
import com.naiterui.ehp.ps.push.bean.MessageRegist;
import com.naiterui.ehp.ps.push.exception.ExceptionCodes;

import lombok.extern.slf4j.Slf4j;

/**
 * IM消息推送工具类
 */
@Slf4j
public class IMnoticeUtil {

    /**
     * 公共IM通知发送：<code>IM_NOTICE_PUBLIC_URL</code>
     */
    private static final String IM_NOTICE_PUBLIC_URL = ConfigUtil.getString("im.notice.public");
    /**
     * 私人IM通知发送：<code>IM_NOTICE_PRIVATE_URL</code>
     */
    private static final String IM_NOTICE_PRIVATE_URL = ConfigUtil.getString("im.notice.private");

    /**
     * 批量发送私人公告
     */
    private static final String IM_NOTICE_GROUP_URL = ConfigUtil.getString("im.notice.group");

    /**
     * IM通知类消息推送(批量)
     * IMnoticeUtil.sendNotice()
     *
     * @param message
     * @param messageRegist
     *
     * @return
     *
     * @throws BusinessException
     * @Date 2016年4月7日
     * @since 2.3.0
     */
    public static void sendNotice(String message, MessageRegist messageRegist) throws BusinessException {

        if (StringUtils.isEmpty(message)) {
            log.error("notice send ERROR ; message={}", message);
            throw new BusinessException(ExceptionCodes.MESSAGE_TARGET_OR_CONTENT_EMPTY);
        }

        Map<String, Object> msgMap = new HashMap<>();
        msgMap.put("i", message);
        msgMap.put("c", messageRegist.getClient());
        msgMap.put("t", messageRegist.getType());

        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("message", JsonMapper.toJson(msgMap));
        paramMap.put("userType", messageRegist.getUserType());
        RestHttpUtil.post().url(IM_NOTICE_PUBLIC_URL).paramMap(paramMap).executeData();
    }

    /**
     * IM通知类消息推送(个人)
     */
    public static void sendNotice(Long userId, String message, MessageRegist messageRegist) throws BusinessException {

        if (null == userId || StringUtils.isEmpty(message)) {
            log.error(" notice send ERROR ; userId={},userType={},message={}", userId, messageRegist.getUserType(), message);
            throw new BusinessException(ExceptionCodes.MESSAGE_TARGET_OR_CONTENT_EMPTY);
        }

        Map<String, Object> msgMap = new HashMap<>();
        msgMap.put("i", message);
        msgMap.put("c", messageRegist.getClient());
        msgMap.put("t", messageRegist.getType());

        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("message", JsonMapper.toJson(msgMap));
        paramMap.put("userId", userId.toString());
        paramMap.put("userType", messageRegist.getUserType());
        // 如果是视频问诊的图片消息则强推
        if (RegisterEnum.VIDEO_CONSULT_PT_IMAGE.getType() == messageRegist.getType()) {
            paramMap.put("force", "true");
        }
        RestHttpUtil.post().url(IM_NOTICE_PRIVATE_URL).paramMap(paramMap).executeData();

    }

    /**
     * 批量发送个人公告
     */
    public static void groupSendAnnouncement(String recipientCacheKey, String message, MessageRegist messageRegist) throws BusinessException {

        if (null == recipientCacheKey || StringUtils.isEmpty(message)) {
            log.error(" groupSendAnnouncement  ERROR ; doctorIdsCacheKey={},userType={},message={}", recipientCacheKey,
                      messageRegist.getUserType(),
                      message);
            throw new BusinessException(ExceptionCodes.MESSAGE_TARGET_OR_CONTENT_EMPTY);
        }

        Map<String, Object> msgMap = new HashMap<>();
        msgMap.put("i", message);
        msgMap.put("c", messageRegist.getClient());
        msgMap.put("t", messageRegist.getType());

        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("message", JsonMapper.toJson(msgMap));
        paramMap.put("recipientCacheKey", recipientCacheKey);
        paramMap.put("userType", messageRegist.getUserType());
        RestHttpUtil.post().url(IM_NOTICE_GROUP_URL).paramMap(paramMap).executeData();
    }
}
